<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Mm_Xml_Element_Parser {
    
    private $attributes = array();
    private $value;
    private $tagname = "";
    
    public function __construct(DOMElement $elm) {
        
        foreach ($elm->attributes as $k => $v) {
            $k = $v->prefix ? $v->prefix.':'.$k : $k;
            $this->attributes[$k] = $v->value;
        }
        if($elm->hasChildNodes() && $elm->childNodes->item(0)->nodeType == 3) { 
            $this->value = trim($elm->childNodes->item(0)->nodeValue);
        } else {
            $this->value = "";
        }
        $this->tagname = $elm->tagName;
    }
    
    public function __set($name, $value) {
        throw new Exception("Can't set property: " . get_class($this) . "->$name");
    }
    
    public function __get($name) {
        
        if($name === 'value')
            return $this->value;
        if($name === 'attributes')
            return $this->attributes;    
        if($name === 'tagname')
            return $this->tagname; 
            
        throw new Exception("Cannot get non-existing property: " . get_class($this) . "->$name");
    }
}